<?php

namespace App\Kuafu\Nsq\Service;

use Illuminate\Support\Facades\Log;
use Kuafu\Nsq\NsqPublisher;

class NsqService
{
    /**
     * @var string
     */
    private string $topic;

    /**
     * @var array
     */
    private array $body;


    /**
     * @return string
     */
    public function getTopic(): string
    {
        return $this->topic;
    }

    /**
     * @param string $topic
     */
    public function setTopic(string $topic): void
    {
        $this->topic = $topic;
    }

    /**
     * @return array
     */
    public function getBody(): array
    {
        return $this->body;
    }

    /**
     * @param array $body
     */
    public function setBody(array $body): void
    {
        $this->body = $body;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function sendNsq()
    {
        try {
            Log::info('NSQ SendJob Send Data', ['topic' => $this->topic, 'body' => $this->body]);
            // 业务逻辑
            $this->body['nsqEventTime'] = time();
            (new NsqPublisher(config('nsq.default.address')))->pub($this->topic, $this->body);
        } catch (\Exception $exception) {
            Log::error('NSQ SendJob Send Data Error', ['topic' => $this->topic, 'body' => $this->body]);
        }
    }

}